package com.edu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.demo.entity.MediumTerm;
import com.edu.demo.entity.Project;
import com.edu.demo.entity.Student;
import com.edu.demo.entity.Teacher;
import com.edu.demo.exception.MultipleException;
import com.edu.demo.exception.MyNullPointerException;
import com.edu.demo.mapper.MediumTermMapper;
import com.edu.demo.mapper.ProjectMapper;
import com.edu.demo.mapper.StudentMapper;
import com.edu.demo.service.IMediumTermService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.demo.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 中期检查 服务实现类
 * </p>
 *
 * @author jixuewei
 * @since 2023-01-12
 */
@Service
public class MediumTermServiceImpl extends ServiceImpl<MediumTermMapper, MediumTerm> implements IMediumTermService {

    @Autowired
    private MediumTermMapper mediumTermMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 提交中期报告
     *
     * @param mediumTerm
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int submitMedium(MediumTerm mediumTerm) throws Exception {
        mediumTerm.setStuId(SecurityUtils.getStudent().getId());
        mediumTerm.setStuName(SecurityUtils.getStudent().getName());
        mediumTerm.setUploadTime(new Date());
        QueryWrapper<MediumTerm> queryWrapper = new QueryWrapper<>();
        List<MediumTerm> mediumTerms = mediumTermMapper.selectList(queryWrapper.eq("pro_id", mediumTerm.getProId()).eq("stu_id", mediumTerm.getStuId()));
        if(mediumTerms.size()>0) throw new MultipleException("周期报告已经提交，你可以选择更新中期报告");
        mediumTermMapper.insert(mediumTerm);
        Project project = Project.builder().id(mediumTerm.getProId()).midTerm(1).build();
        if(!updateProject(project)){
            throw new MyNullPointerException("没有该project");
        }
        return 1;
    }


    /**
     * 查看某个项目的周期报告
     *
     * @param projectId
     * @return
     */
    @Override
    public MediumTerm selectMedium(Integer projectId) {

        Project project = projectMapper.selectById(projectId);
        if(!Objects.isNull(project)){
            QueryWrapper<MediumTerm> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pro_id",projectId);
            MediumTerm mediumTerm = mediumTermMapper.selectOne(queryWrapper);
            if(!Objects.isNull(mediumTerm)){
                Student student1 = SecurityUtils.getStudent();
                if(student1!=null){
                    if(student1.getId() != mediumTerm.getStuId()){
                        throw new BadCredentialsException("只能查询自己的周期检查");
                    }
                }
                Student student = studentMapper.selectById(mediumTerm.getStuId());
                student.setPassword(null);
                mediumTerm.setStudent(student);
                mediumTerm.setProject(project);
                return mediumTerm;
            }
        }
        return null;
    }


    /**
     * 更新中期报告
     *
     * @param mediumTerm
     * @return
     */
    @Override
    public int updateMedium(MediumTerm mediumTerm) {
        Student student = SecurityUtils.getStudent();
        mediumTerm.setStuId(student.getId());
        mediumTerm.setStuName(student.getName());
        int i = mediumTermMapper.updateById(mediumTerm);
        return i;
    }

    /**
     * 教师将中期检查改为已阅
     *
     * @param mediumTerm
     * @return
     */
    @Override
    public int checkedMedium(MediumTerm mediumTerm) {
        Teacher teacher = SecurityUtils.getTeacher();

        List<Project> projectList = projectMapper.selectList(new QueryWrapper<Project>().eq("t_id", teacher.getId()));
        List<Integer> collect = projectList.stream().map(project -> project.getId()).collect(Collectors.toList());
        if(!collect.contains(mediumTerm.getProId())){ //不是自己的项目
            return -22;
        }
        MediumTerm mediumTerm1 = new MediumTerm();
        mediumTerm1.setId(mediumTerm.getId());
        mediumTerm1.setStatus(1);
        return  mediumTermMapper.updateById(mediumTerm1);

    }

    private boolean updateProject(Project pro){
        int i = projectMapper.updateById(pro);
        return i==1?true:false;
    }
}
